package org.seckill.web;

import java.util.Date;
import java.util.List;

import org.seckill.dto.Exposer;
import org.seckill.dto.ReturnResult;
import org.seckill.dto.SeckillExcution;
import org.seckill.entity.Seckill;
import org.seckill.exception.RepeatKillException;
import org.seckill.exception.SeckillCloseException;
import org.seckill.exception.SeckillException;
import org.seckill.service.SeckillService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
//@RequestMapping("/seckill")
public class SeckillAction {
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	@Autowired
	private SeckillService seckillService;
	/**
	 * 秒杀列表页
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/list",method=RequestMethod.GET)
	public String getList(Model model){
		List<Seckill> list=seckillService.getSeckillList();
		logger.info("list");
		model.addAttribute("list", list);
		
		return "list";
	}
	/**
	 * 详情页
	 * @param seckillId
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/detail/{seckillId}",method=RequestMethod.GET)
	public String getDetail(@PathVariable("seckillId")long seckillId,Model model){
		logger.info("detail");
		Seckill seckill=seckillService.getById(seckillId);
		model.addAttribute(seckill);
		
		return "detail";
	}
	/**
	 * 获取服务器时间
	 * @return
	 */
	@RequestMapping(value="/serverTime",method=RequestMethod.GET)
	@ResponseBody
	public ReturnResult<Long> getServerTime(){
		Date date=new Date();
		return new ReturnResult<Long>(200,"success",date.getTime());
	}
	/**
	 * 暴露接口地址
	 * @param seckillId
	 * @param userPhone
	 * @return
	 */
	@RequestMapping(value="/exposer/{seckillId}",method=RequestMethod.POST)
	@ResponseBody
	public ReturnResult<Exposer> getExposerUrl(@PathVariable("seckillId")long seckillId){
		try{
			Exposer exposer=seckillService.exportSeckillUrl(seckillId);
			return new ReturnResult<Exposer>(200, "success",exposer);
		}catch(Exception e){
			return new ReturnResult<Exposer>(202, "fail");
		}
		
	}
	/**
	 * 秒杀
	 * @param seckillId
	 * @param md5
	 * @param userPhone
	 * @return
	 */
	@RequestMapping(value="/execution/{seckillId}/{md5}",method=RequestMethod.POST)
	@ResponseBody
	public ReturnResult<SeckillExcution> executeSeckill(@PathVariable("seckillId")long seckillId,@PathVariable("md5")String md5,@CookieValue("userPhone")long userPhone){
		try{
			SeckillExcution seckillExcution=seckillService.executeSeckill(seckillId, userPhone, md5);
			return new ReturnResult<SeckillExcution>(200,"success",seckillExcution);
		}catch(RepeatKillException e){
			return new ReturnResult<SeckillExcution>(202,e.getMessage());
		}catch(SeckillCloseException e){
			return new ReturnResult<SeckillExcution>(202,e.getMessage());
		}catch(SeckillException e){
			return new ReturnResult<SeckillExcution>(202,e.getMessage());
		}catch(Exception e){
			return new ReturnResult<SeckillExcution>(202,e.getMessage());
		}
		
	}
	/**
	 * 利用存储过程来秒杀
	 * @param seckillId
	 * @param md5
	 * @param userPhone
	 * @return
	 */
	@RequestMapping(value="/execution1/{seckillId}/{md5}",method=RequestMethod.POST)
	@ResponseBody
	public ReturnResult<SeckillExcution> executeSeckill1(@PathVariable("seckillId")long seckillId,@PathVariable("md5")String md5,@CookieValue("userPhone")long userPhone){
		try{
			SeckillExcution seckillExcution=seckillService.executeSeckillByProc(seckillId, userPhone, md5);
			return new ReturnResult<SeckillExcution>(200,"success",seckillExcution);
		}catch(RepeatKillException e){
			return new ReturnResult<SeckillExcution>(202,e.getMessage());
		}catch(SeckillCloseException e){
			return new ReturnResult<SeckillExcution>(202,e.getMessage());
		}catch(SeckillException e){
			return new ReturnResult<SeckillExcution>(202,e.getMessage());
		}catch(Exception e){
			return new ReturnResult<SeckillExcution>(202,e.getMessage());
		}
		
	}
}
